#include "DataParse.h"

u8 XorByteGen(u8 *data, int len)
{
    u8 result = 0;
    for (int i = 0; i < len; i++)
    {
        result = result ^ *(data + i);
    }
    return result;
}

u8 *GenCommonData(u8 cmd, u8 *data, int len)
{
    u8 *result = (u8 *)malloc(len + 4);
    memset(result, 0, len + 4);
    result[0] = DATA_HEAD;
    result[1] = len + 2;
    result[2] = cmd;
    memcpy(result + 3, data, len);
    result[len + 3] = XorByteGen(result, len + 3);
    return result;
}

u8 GenSetTimeData(u8 hour, u8 minute, u8 second, u8 **out)
{
    u8 time[3] = {hour, minute, second};
    *out = GenCommonData(CMD_SET_TIME, time, 3);
    return 7;
}

u8 GenSetConfData(DeviceConf *deviceConf, u8 **out)
{
    *out = GenCommonData(CMD_SET_CONF, (u8 *)deviceConf, sizeof(DeviceConf));
    return (sizeof(DeviceConf) + 4);
}

// u8 *GenSwitchMode(u8 mode)
// {
// }